import ajax from '../libs/ajax.js';
import tools from '../libs/tools.js';

//#region 发布评论
let bt = document.getElementById('bt');
let nr = document.getElementById('nr');
let fb = document.getElementById('fb');

fb.addEventListener('click', () => {
  let info = {
    title: bt.value,
    info: nr.value
  };
  ajax.send('/message/add', info, (data) => {
    if (data.success) {
      showToast(data.message);
      nr.value = '';
      bt.value = '';
      location.href = './twtwz.html';
    }
  });
});

//#endregion

//#region 首页
let sy = document.getElementById('sy');
sy.addEventListener('click', () => {
  location.href = './twtwz.html';
});
//#endregion

//#region 获取用户信息
let tbUser = {};
let tbUserInfo = {};
let userOtherInfo = {};

function queryUserInfo() {
  ajax.send('/user/auth/getUserInfo', {}, (data) => {
    if (data.success) {
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      userOtherInfo = data.userOtherInfo;
      showUser();
    } else {
      showToast('用户未登录');
    }
  });
}
//#endregion
let imgLogo1 = document.getElementById('imgLogo1');
let imgLogo2 = document.getElementById('imgLogo2');
//#region 展示用户信息
function showUser() {
  // 处理用户头像
  if (tbUserInfo.img != '') {
    imgLogo1.setAttribute('src', tbUserInfo.img);
    imgLogo2.setAttribute('src', tbUserInfo.img);
  }
}
//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

queryUserInfo();
